/*
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: 10/06/2004
 * Time: 20:19:41
 */
package com.talios.jira.idea.intentions;

import com.intellij.codeInsight.intention.IntentionManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.WindowManager;
import com.intellij.psi.PsiFile;
import com.intellij.util.IncorrectOperationException;
import com.talios.jira.browser.JiraItem;
import com.talios.jira.idea.JiraConfigurationComponent;

/**
 * @jira INTPLUG-88 Add intention to work on issue
 */
public class JiraWorkOnIssueIntention extends AnnotatedIssueCommentIntention {
    private static final Logger LOGGER = Logger.getInstance("Jira Browser");

    private final Project myProject;

    public JiraWorkOnIssueIntention(Project project) {
        myProject = project;
    }

    public void projectOpened() {
    }

    public void projectClosed() {
    }

    public String getComponentName() {
        return "JiraWorkOnIssueIntention";
    }

    public void initComponent() {
        IntentionManager.getInstance(myProject).addAction(this);
    }

    public void disposeComponent() {
    }

    public String getText() {
        return "Work on issue " + issueKey;
    }

    public String getFamilyName() {
        return getText();
    }

    /**
     * @jira INTPLUG-88 Add intention to work on issue
     */
    public void invoke(Project project, Editor editor, PsiFile psiFile) throws IncorrectOperationException {

        JiraConfigurationComponent config = project.getComponent(JiraConfigurationComponent.class);
        JiraItem item = findAnnotatedIssue(editor, psiFile, project);

        if (item != null) {
            config.setItemWorkedOn(item);
            WindowManager.getInstance().getStatusBar(project).setInfo("Now working on issue " + item.getKey() + ": " + item.getTitle());
            LOGGER.info("Working on issue " + issueKey);
            item.startWorkingOnIssue();
        }

    }

    public boolean startInWriteAction() {
        return false;
    }
}
